package com.ayuku.commons.business.user;

import java.util.GregorianCalendar;
import java.util.List;

import com.ayuku.commons.AyukuDB;
import com.ayuku.commons.UtilServer;
import com.ayuku.retail.persistence.model.AppFunctions;
import com.ayuku.retail.persistence.model.AppUserGroupFunc;
import com.ayuku.retail.persistence.model.AppUsers;

public class UserImpl {
	public static List<AppUsers> getList(String arg) throws Exception {
		List<AppUsers> lst = AyukuDB
				.getPersist()
				.readList(
						AppUsers.class,
						"SELECT * FROM app_users WHERE ucase(trim(uname)) LIKE ucase(?) or ucase(trim(name)) LIKE ucase(?)",
						new Object[] { arg, arg });
		return lst;
	}

	public static AppUsers getById(String key) throws Exception {
		AppUsers obj = (AppUsers) AyukuDB.getPersist().readByPrimaryKey(
				AppUsers.class, new Object[] { key });
		return obj;
	}

	public static AppUsers doLogin(String username, String password)
			throws Exception {

		if (username != null
				&& "administrator".equalsIgnoreCase(username.trim())) {
			setMenu();
		}

		String md5pwd = UtilServer.getMd5(password.trim());
		AppUsers obj = (AppUsers) AyukuDB
				.getPersist()
				.read(
						AppUsers.class,
						"SELECT * FROM app_users WHERE uname = ? AND upass = ?",
						new Object[] { username, md5pwd });
		return obj;
	}

	public static int add(AppUsers user) throws Exception {
		String md5pwd = UtilServer.getMd5(user.getUpass());
		user.setUpass(md5pwd);
		setModifiedDate(user);
		return AyukuDB.getPersist().insert(user);
	}

	public static int update(AppUsers user) throws Exception {
		String md5pwd = UtilServer.getMd5(user.getUpass());

		user.setUpass(md5pwd);
		setModifiedDate(user);
		return AyukuDB.getPersist().update(user);
	}

	public static int delete(String key) throws Exception {
		if ("administrator".equals(key.trim())) {
			return 1;
		}
		int n = AyukuDB.getPersist().executeUpdate(
				"delete from app_users where trim(UNAME) = ?",
				new Object[] { key.trim() });
		return n;
	}

	public static int delete(List<String> ids) throws Exception {
		int n = 0;
		try {
			AyukuDB.getPersist().setAutoCommit(false);
			for (int j = 0; j < ids.size(); j++) {
				String key = ids.get(j);
				n += delete(key);
			}
			AyukuDB.getPersist().commit();
		} catch (Exception e) {
			e.printStackTrace();
			AyukuDB.getPersist().rollback();			
			throw new Exception();
		} finally {
			AyukuDB.getPersist().setAutoCommit(true);
		}
		return n;
	}

	private static void setModifiedDate(AppUsers pjo) throws Exception {
		pjo.setModifiedDate(UtilServer.getCurrentDate());
		if (pjo.getCreatedDate() == null) {
			pjo.setCreatedDate(UtilServer.getCurrentDate());
		}
	}

	private static void setMenu() throws Exception {
		List<AppFunctions> lst = AyukuDB.getPersist().readList(
				AppFunctions.class,
				"SELECT * FROM app_functions WHERE id is not null");

		AyukuDB.getPersist().executeUpdate(
				"delete from app_user_group_func where group_id = 'master'");

		for (int i = 0; i < lst.size(); i++) {
			AppFunctions str = lst.get(i);
			AppUserGroupFunc func = new AppUserGroupFunc();
			func.setGroupId("master");
			func.setFuncId(str.getId());
			func.setAccessAdd("true");
			func.setAccessDelete("true");
			func.setAccessEdit("true");
			func.setAccessView("true");
			func.setCreatedDate(new GregorianCalendar().getTime());
			func.setCreatedBy("master");
			func.setModifiedBy("master");
			AyukuDB.getPersist().insert(func);
		}

	}
}
